#include "EncDec.h"

//pdat=需要加密的数据段地址,dwnum=DWORD个数(通常0x400),pkey="Vampire.C.J at Softstar Technology (ShangHai) Co., Ltd"
void EncIndex(DWORD* pdat, DWORD dwnum, DWORD* pkey) {

	DWORD esp10 = (DWORD)(0 - 0x61C88647);
	do {
		DWORD index = 0;
		DWORD fix1 = (esp10 / 0x04) & 0x03;
		DWORD dat1;
		DWORD dat2;
		DWORD nDat;
		for (size_t i = 0; i < dwnum; i++)
		{
			dat1 = pdat[(index - 1) % dwnum];
			dat2 = pdat[(index + 1) % dwnum];
			nDat = pdat[index] + ((((dat2 * 0x4) ^ (dat1 / 0x20)) + ((dat2 / 0x8) ^ (dat1 * 0x10))) ^ ((esp10 ^ dat2) + (pkey[(index & 3) ^ fix1] ^ dat1)));
			pdat[index] = nDat;
			index++;
		}
		esp10 -= 0x61C88647;
	} while (esp10 != ((DWORD)floor((double)(52.00f / (float)dwnum + 6.00f)) * 0x9E3779B9) - 0x61C88647);

	return;
}

//pdat=需要解密的数据段地址,dwnum=DWORD个数(通常0x400),pkey="Vampire.C.J at Softstar Technology (ShangHai) Co., Ltd"
void DecIndex(DWORD* pdat, DWORD dwnum, DWORD* pkey)
{
	DWORD esp10 = (DWORD)floor((double)(52.00f / (float)dwnum + 6.00f)) * 0x9E3779B9;

	do {
		DWORD index = dwnum - 0x1;
		DWORD fix1 = (esp10 / 0x04) & 0x03;
		DWORD dat1;
		DWORD dat2;
		DWORD nDat;
		for (size_t i = 0; i < dwnum; i++)
		{
			dat1 = pdat[(index - 1) % dwnum];
			dat2 = pdat[(index + 1) % dwnum];
			nDat = pdat[index] - ((((dat2 * 0x4) ^ (dat1 / 0x20)) + ((dat2 / 0x8) ^ (dat1 * 0x10))) ^ ((esp10 ^ dat2) + (pkey[(index & 3) ^ fix1] ^ dat1)));
			//fdat[0] - ((((fdat2*4 ^ fdat1/0x20) + (fdat2/0x8 ^ fdat1*0x10))) ^ ((esp10 ^ fdat2) + (key[(index&3) ^ fix1] ^ fdat1)))

			pdat[index] = nDat;
			index--;
		}
		esp10 += 0x61C88647;
	} while (esp10);
	return;
}
